<template>
    <div>
        <a-modal title="合并需求" v-model:visible="firstVisible">
            <div>
                <div class="title">请选择一个需求，其他需求将合并到该需求</div>
                <div class="option-group">
                    <a-radio-group v-model:value="selectedItem">
                        <a-radio v-for="item of list" :value="item.requestName" :key="item.id" :style="radioStyle">
                            <a-tooltip placement="left" :title="item.requestName">
                                {{ item.requestName }}
                            </a-tooltip>
                        </a-radio>
                    </a-radio-group>
                </div>
            </div>
            <template #footer>
                <a-button key="back" @click="firstVisible = false">取消</a-button>
                <a-button key="submit" type="primary" @click="handleNext">下一步</a-button>
            </template>
        </a-modal>
        <a-modal title="合并需求" v-model:visible="secondVisible">
            <div>
                <div class="title">
                    <span>以下需求将合并至：</span>
                    <span class="active">
                        {{ selectedItem }}
                    </span>
                </div>
                <div class="option-group">
                    <div v-for="item of otherList" :key="item.id">
                        <a-badge color="#000" :text="item.requestName" v-fitText />
                    </div>
                </div>
            </div>
            <template #footer>
                <a-button key="back" @click="handlePrev">上一步</a-button>
                <a-popconfirm title="合并操作不可撤回，您确定要合并吗？" ok-text="确定" cancel-text="取消" @confirm="handleMerge">
                    <a-button key="submit" type="primary">确定合并</a-button>
                </a-popconfirm>
            </template>
        </a-modal>
    </div>
</template>

<script lang="ts">
import api from '@/api';
import { message } from 'ant-design-vue';
import { defineComponent, reactive, toRefs, PropType } from 'vue';
interface BasicState {
    firstVisible: boolean;
    secondVisible: boolean;
    selectedItem: string; // 合并的需求名称
    otherList: any[];
    radioStyle: any;
}
export default defineComponent({
    props: {
        list: {
            type: Array as PropType<any[]>,
            default: () => []
        }
    },
    emits: ['success'],
    setup(props, { emit }) {
        const state = reactive<BasicState>({
            firstVisible: false,
            secondVisible: false,
            selectedItem: '', // 合并的需求名称
            otherList: [],
            radioStyle: {
                display: 'block',
                height: '30px',
                lineHeight: '30px'
            }
        });

        const open = () => {
            state.firstVisible = true;
            state.selectedItem = props.list[0].requestName;
        };
        // 上一步
        const handlePrev = () => {
            state.secondVisible = false;
            state.firstVisible = true;
        };
        // 下一步
        const handleNext = () => {
            state.firstVisible = false;
            state.secondVisible = true;
            state.otherList = props.list.filter((item) => item.requestName !== state.selectedItem);
        };
        const handleMerge = async () => {
            const ids: any[] = [];
            let id = 0;
            props.list.forEach((item) => {
                if (item.requestName !== state.selectedItem) {
                    ids.push(item.id);
                } else {
                    id = item.id;
                }
            });
            const query = {
                id,
                ids
            };
            const { success, errorMsg } = await api.pmpRequestInfoModel.merge(query);
            if (success) {
                emit('success');
                message.success('合并成功');
            } else {
                message.error(errorMsg);
            }
            state.secondVisible = false;
        };
        return {
            ...toRefs(state),
            open,
            handlePrev,
            handleNext,
            handleMerge
        };
    }
});
</script>
<style scoped lang="less">
.title {
    font-size: 16px;
    color: #172b4d;
    font-weight: 500;
    .active {
        color: #dd4d40;
    }
}
.option-group {
    margin: 24px 0 0 24px;
}
::v-deep(.ant-radio-wrapper) {
    width: 400px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
</style>